# coding:utf-8
import requests
import codecs

# 先使用浏览器登录网站，然后从请求信息中提取url、payload、cookies
url = 'http://127.0.0.1:8000/login/'

headers_str='''
Host: www.tianyancha.com
Connection: keep-alive
Accept: */*
Origin: https://www.tianyancha.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Content-Type: application/json; charset=UTF-8
Referer: https://www.tianyancha.com/login
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.8
'''
cookies_str = '''
TYCID=fa698530b88511e7a1a683210f8281cf; uccid=d8d0eefba6a3aa5caaa4d99c67b49917; ssuid=1942724536; tyc-user-info=%257B%2522token%2522%253A%2522eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzI3MTMyNTM2NiIsImlhdCI6MTUwODgzOTUzNCwiZXhwIjoxNTI0MzkxNTM0fQ.4maK9LbNiQnByNSwV05g1pAZTjtHABG35IWxNSzyjUno7q2VIZ3H68QjZJ0D8fFZrolFnPShnj9GlJ1UK28wXg%2522%252C%2522integrity%2522%253A%25220%2525%2522%252C%2522state%2522%253A%25220%2522%252C%2522vnum%2522%253A%25220%2522%252C%2522onum%2522%253A%25220%2522%252C%2522mobile%2522%253A%252213271325366%2522%257D; auth_token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzI3MTMyNTM2NiIsImlhdCI6MTUwODgzOTUzNCwiZXhwIjoxNTI0MzkxNTM0fQ.4maK9LbNiQnByNSwV05g1pAZTjtHABG35IWxNSzyjUno7q2VIZ3H68QjZJ0D8fFZrolFnPShnj9GlJ1UK28wXg; Hm_lvt_e92c8d65d92d534b0fc290df538b4758=1508834272,1508837510,1508838535,1508838725; Hm_lpvt_e92c8d65d92d534b0fc290df538b4758=1508839550; _csrf=/JRQ914yANIoZkQOWPPtUA==; OA=9ECxAy2vah8G1BJcVCnardkbOzCq6oTSjgVXWzpptfT8lLfupC+tZrAGQeq1hKGy+u2UaVwzVSW2F8Lqk2uAfp4McFO77wLvKZRDZiZZaes=; _csrf_bk=c1ed50ad7c090bfc9b4c65ab1480bd17
'''

headers = {item.split(':')[0]:''.join(item.split(':')[1:]).strip() for item in headers_str.split('\n') if item}
cookies = {item.split(':')[0]:''.join(item.split('=')[1:]).strip() for item in cookies_str.split('\n') if item}

payload = {
    'key':'中牟',
    'checkFrom':'searchBox'
}
r =  requests.get('https://www.tianyancha.com/search',params=payload,headers=headers,cookies=cookies,verify=False)

html_doc =  r.content.decode(r.encoding)
with codecs.open('tianyancha.html','w',r.encoding) as f:
    f.write(html_doc)